<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_controller extends admin_controller
{
    public function __construct()
    {
        parent::__construct();

        $this->page_title     = 'Người dùng';
        $this->table_name     = 'user';
        $this->route_name     = 'user_controller';
        $this->business_model = $this->user;
    }

    public function index()
    {
        $user = $this->get_logged_admin_account();
        if ($user->user_group_code != 'SUPER_ADMIN') {
            echo 'You do not have permission to access this page';
            return;
        }

        $header_data              = array();
        $header_data['show_menu'] = true;
        $view_data                = $this->get_view_data();
        $view_data['list']        = $this->get_list();
        $view_data['actions']     = $this->get_list_actions();

        $this->load_view('list_template', $header_data, $view_data);
    }


    public function get_post_data()
    {
        $post_data = array(
            'username'        => $this->input->post('username'),
            'password'        => $this->input->post('password'),
            'user_group_code' => $this->input->post('user_group_code'),
            'note'            => $this->input->post('note'),
            'modified_date'   => time()

        );
        return $post_data;
    }

    public function get_list()
    {
        $list = array();
        $news = $this->business_model->get_elements();
        foreach ($news as $item) {
            $actions   = array();
            $actions[] = array(
                'text' => 'Sửa',
                'href' => base_url() . $this->base_admin_url . $this->route_name . '/update/' . $item->id
            );
            $list[]    = array(
                'can_delete'      => $item->id,
                'username'        => $item->username,
                'password'        => $item->password,
                'user_group_code' => $item->user_group_code == 'SUPER_ADMIN' ? 'Cấp cao' : 'Cấp thấp',
                'actions'         => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list   = array();
        $list[] = 'Tên đăng nhập';
        $list[] = 'Mật khẩu';
        $list[] = 'Nhóm người dùng';
        $list[] = 'Công việc';
        return $list;
    }

    public function get_fields($id)
    {
        if ($id == -1) {
            $model                  = new stdClass();
            $model->username        = '';
            $model->password        = '';
            $model->user_group_code = '';
            $model->note            = '';
        } else {
            $model = $this->business_model->get_element_by_id($id);
        }
        $fields   = array();
        $fields[] = array(
            'text'  => 'Tên đăng nhập',
            'name'  => 'username',
            'value' => $model->username,
            'type'  => 'text'
        );
        $fields[] = array(
            'text'  => 'Mật khẩu',
            'name'  => 'password',
            'value' => $model->password,
            'type'  => 'text'
        );
        $fields[] = array(
            'text'            => 'Nhóm người dùng',
            'name'            => 'user_group_code',
            'value'           => $model->user_group_code,
            'type'            => 'select',
            'options'         => array(
                array_to_object(array(
                    'text'  => 'Cấp thấp',
                    'value' => 'ADMIN'
                )),
                array_to_object(array(
                    'text'  => 'Cấp cao',
                    'value' => 'SUPER_ADMIN'
                ))
            ),
            'select_display'  => 'text',
            'select_value'    => 'value',
            'select_selected' => $model->user_group_code
        );
        return $fields;
    }
}